﻿using System;
using System.Web;
using System.Configuration;

namespace ECommerce
{
    public class CommonHelper
    {
        /// <summary>
        /// Gets query string value by name
        /// </summary>
        /// <param name="name">Parameter name</param>
        /// <returns>Query string value</returns>
        public static string QueryString(string name)
        {
            string result = string.Empty;
			if (HttpContext.Current != null && HttpContext.Current.Request.QueryString[name] != null)
			{
				result = HttpContext.Current.Request.QueryString[name].ToString();
			}
            return result;
        }

        /// <summary>
        /// Gets integer value from query string 
        /// </summary>
        /// <param name="name">Parameter name</param>
        /// <returns>Query string value</returns>
        public static int QueryStringInt(string name)
        {
            string resultStr = QueryString(name).ToUpperInvariant();
            int result = 0;
            Int32.TryParse(resultStr, out result);
            return result;
        }

        /// <summary>
        /// Gets store location
        /// </summary>
        /// <returns>Store location</returns>
        public static string GetStoreLocation()
        {
            bool useSSL = IsCurrentConnectionSecured();
            return GetStoreLocation(useSSL);
        }

        /// <summary>
        /// Gets a value indicating whether current connection is secured
        /// </summary>
        /// <returns>true - secured, false - not secured</returns>
        public static bool IsCurrentConnectionSecured()
        {
            bool useSSL = false;
            if (HttpContext.Current != null && HttpContext.Current.Request != null)
            {
                useSSL = HttpContext.Current.Request.IsSecureConnection;
                //when your hosting uses a load balancer on their server then the Request.IsSecureConnection is never got set to true, use the statement below
                //just uncomment it
                //useSSL = HttpContext.Current.Request.ServerVariables["HTTP_CLUSTER_HTTPS"] == "on" ? true : false;
            }

            return useSSL;
        }

        /// <summary>
        /// Gets store location
        /// </summary>
        /// <param name="useSsl">Use SSL</param>
        /// <returns>Store location</returns>
        public static string GetStoreLocation(bool useSsl)
        {
            string result = GetStoreHost(useSsl);
            if (result.EndsWith("/"))
                result = result.Substring(0, result.Length - 1);
            result = result + HttpContext.Current.Request.ApplicationPath;
            if (!result.EndsWith("/"))
                result += "/";

            return result.ToLowerInvariant();
        }

        /// <summary>
        /// Gets store host location
        /// </summary>
        /// <param name="useSsl">Use SSL</param>
        /// <returns>Store host location</returns>
        public static string GetStoreHost(bool useSsl)
        {
            string result = "http://" + ServerVariables("HTTP_HOST");
            if (!result.EndsWith("/"))
                result += "/";

            if (useSsl)
            {
                if (!String.IsNullOrEmpty(ConfigurationManager.AppSettings["SharedSSL"]))
                {
                    result = ConfigurationManager.AppSettings["SharedSSL"];
                }
                else
                {
                    result = result.Replace("http:/", "https:/");
                }
            }

            if (!result.EndsWith("/"))
                result += "/";

            return result.ToLowerInvariant();
        }

        /// <summary>
        /// Gets server variable by name
        /// </summary>
        /// <param name="name">Name</param>
        /// <returns>Server variable</returns>
        public static string ServerVariables(string name)
        {
            string tmpS = string.Empty;
            try
            {
                if (HttpContext.Current.Request.ServerVariables[name] != null)
                {
                    tmpS = HttpContext.Current.Request.ServerVariables[name].ToString();
                }
            }
            catch
            {
                tmpS = string.Empty;
            }
            return tmpS;
        }

		/// <summary>
		/// Gets integer value from cookie
		/// </summary>
		/// <param name="cookieName">Cookie name</param>
		/// <returns>Result</returns>
		public static int GetCookieInt(string cookieName)
		{
			string str1 = GetCookieString(cookieName, true);
			if (!String.IsNullOrEmpty(str1))
				return Convert.ToInt32(str1);
			else
				return 0;
		}

		/// <summary>
		/// Gets cookie string
		/// </summary>
		/// <param name="cookieName">Cookie name</param>
		/// <param name="decode">Decode cookie</param>
		/// <returns>Cookie string</returns>
		public static string GetCookieString(string cookieName, bool decode)
		{
			if (HttpContext.Current.Request.Cookies[cookieName] == null)
			{
				return string.Empty;
			}
			try
			{
				string tmp = HttpContext.Current.Request.Cookies[cookieName].Value.ToString();
				if (decode)
					tmp = HttpContext.Current.Server.UrlDecode(tmp);
				return tmp;
			}
			catch
			{
				return string.Empty;
			}
		}
	}
}
